#!/bin/sh
#How to use the function?
#This script need three argument.
#arg1:It is a directory. You will generate the module in the directory.
#arg2:It is the module name which you want to generate.
#arg3:It is your email.
#For example:
#./autogen_moudle.sh device s3c6410_mfc xq2537@gmail.com

PREFIX_DIR=$1
MODULE_NAME=$2
AUTHOR=$3

echo "create module $MODULE_NAME in $PREFIX_DIR"

if [ $PREFIX_DIR ]
then
	if [ $PREFIX_DIR = "device" ]
	then
		cd $PREFIX_DIR
		mkdir $MODULE_NAME
		MODULE_HEADER=${MODULE_NAME}/${MODULE_NAME}.h
		MODULE_FILE=${MODULE_NAME}/${MODULE_NAME}_module.c
		SOURCE_FILE=${MODULE_NAME}/${MODULE_NAME}.c
		MAKEFILE_AM=Makefile.am
		touch ${MODULE_HEADER}
		touch ${MODULE_NAME}/${MODULE_NAME}.c
		touch ${MODULE_NAME}/${MODULE_NAME}_module.c

		#Add the module into Makefile.am
		echo "dev_${MODULE_NAME} = ${MODULE_NAME}/${MODULE_NAME}.c ${MODULE_NAME}/${MODULE_NAME}_module.c" >> ${MAKEFILE_AM}
		echo "lib${MODULE_NAME}_la_SOURCES = \$(dev_${MODULE_NAME})" >> ${MAKEFILE_AM}
		echo "lib${MODULE_NAME}_la_LDFLAGS = -module" >> ${MAKEFILE_AM}
		echo "pkglib_LTLIBRARIES += lib${MODULE_NAME}.la" >> ${MAKEFILE_AM}

		# Create the header file for device
		echo -e "/* Copyright (C)
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}.h
* @brief The definition of system controller 
* @author ${AUTHOR}
* @version 78.77
*/

/* Autogenerated by SkyEye script */
#ifndef __${MODULE_NAME}_H__
#define __${MODULE_NAME}_H__

typedef struct ${MODULE_NAME}_reg{
}${MODULE_NAME}_reg_t;

typedef struct ${MODULE_NAME}_device{
	conf_object_t* obj;
	${MODULE_NAME}_reg_t* regs;
}${MODULE_NAME}_device;

#endif" >> ${MODULE_HEADER}

		# Create the source code of module definition file
		echo -e "/* Copyright (C) 
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}_module.c
* @brief ${MODULE_NAME} module for
* @author ${AUTHOR}
* @version 78.77
*/

#include <skyeye_module.h>

const char* skyeye_module = \"${MODULE_NAME}\";

extern void init_${MODULE_NAME}();

void module_init(){
	init_${MODULE_NAME}();
}

void module_fini(){
}" >> ${MODULE_FILE}

		# Create source code of device implementation
		echo -e "/* Copyright (C) 
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}.c
* @brief The implementation of system controller
* @author ${AUTHOR}
* @version 78.77
*/

#include <skyeye_types.h>
#include <skyeye_sched.h>
#include <skyeye_signal.h>
#include <skyeye_class.h>
#include <skyeye_interface.h>
#include <skyeye_obj.h>
#include <skyeye_mm.h> 
#include <memory_space.h>
#include <skyeye_device.h>
#define DEBUG
#include <skyeye_log.h>

#include \"${MODULE_NAME}.h\"

static exception_t ${MODULE_NAME}_read(conf_object_t *opaque, generic_address_t offset, void* buf, size_t count)
{
	struct ${MODULE_NAME}_device *dev = opaque->obj;
	${MODULE_NAME}_reg_t* regs = dev->regs;
	switch(offset) {
		default:
			printf(\"Can not read the register at 0x%x in ${MODULE_NAME}\\\n\", offset);
			return Invarg_exp;
	}
	return No_exp;

}

static exception_t ${MODULE_NAME}_write(conf_object_t *opaque, generic_address_t offset, uint32_t* buf, size_t count)
{
	struct ${MODULE_NAME}_device *dev = opaque->obj;
	${MODULE_NAME}_reg_t* regs = dev->regs;
	uint32_t val = *(uint32_t*)buf;
	switch(offset) {
		default:
			printf(\"Can not write the register at 0x%x in ${MODULE_NAME}\\\n\", offset);
			return Invarg_exp;
	}
	return No_exp;

}
static conf_object_t* new_${MODULE_NAME}(char* obj_name){
	${MODULE_NAME}_device* dev = skyeye_mm_zero(sizeof(${MODULE_NAME}_device));
	${MODULE_NAME}_reg_t* regs =  skyeye_mm_zero(sizeof(${MODULE_NAME}_reg_t));
	dev->obj = new_conf_object(obj_name, dev);
	/* init ${MODULE_NAME} regs */
	dev->regs = regs;

	/* Register io function to the object */
	memory_space_intf* io_memory = skyeye_mm_zero(sizeof(memory_space_intf));
	io_memory->conf_obj = dev->obj;
	io_memory->read = ${MODULE_NAME}_read;
	io_memory->write = ${MODULE_NAME}_write;
	SKY_register_interface(io_memory, obj_name, MEMORY_SPACE_INTF_NAME);	
	return dev->obj;
}
void free_${MODULE_NAME}(conf_object_t* dev){
	
}

void init_${MODULE_NAME}(){
	static skyeye_class_t class_data = {
		.class_name = \"${MODULE_NAME}\",
		.class_desc = \"${MODULE_NAME}\",
		.new_instance = new_${MODULE_NAME},
		.free_instance = free_${MODULE_NAME},
		.get_attr = NULL,
		.set_attr = NULL
	};

	SKY_register_class(class_data.class_name, &class_data);
}" >> ${SOURCE_FILE}

	elif [ $PREFIX_DIR = "utils" ]
	then
		cd $PREFIX_DIR
		mkdir $MODULE_NAME
		MODULE_HEADER=${MODULE_NAME}/${MODULE_NAME}.h
		MODULE_FILE=${MODULE_NAME}/${MODULE_NAME}_module.c
		SOURCE_FILE=${MODULE_NAME}/${MODULE_NAME}.c
		MAKEFILE_AM=Makefile.am
		touch ${MODULE_HEADER}
		touch ${MODULE_NAME}/${MODULE_NAME}.c
		touch ${MODULE_NAME}/${MODULE_NAME}_module.c

		#Add the module into Makefile.am
		echo "dev_${MODULE_NAME} = ${MODULE_NAME}/${MODULE_NAME}.c ${MODULE_NAME}/${MODULE_NAME}_module.c" >> ${MAKEFILE_AM}
		echo "lib${MODULE_NAME}_la_SOURCES = \$(dev_${MODULE_NAME})" >> ${MAKEFILE_AM}
		echo "lib${MODULE_NAME}_la_LDFLAGS = -module" >> ${MAKEFILE_AM}
		echo "pkglib_LTLIBRARIES += lib${MODULE_NAME}.la" >> ${MAKEFILE_AM}

		# Create the header file for device
		echo -e "/* Copyright (C)
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}.h
* @brief The definition of system controller 
* @author ${AUTHOR}
* @version 78.77
*/

/* Autogenerated by SkyEye script */
#ifndef __${MODULE_NAME}_H__
#define __${MODULE_NAME}_H__

#endif" >> ${MODULE_HEADER}

		# Create the source code of module definition file
		echo -e "/* Copyright (C) 
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}_module.c
* @brief ${MODULE_NAME} module for
* @author ${AUTHOR}
* @version 78.77
*/

#include <skyeye_module.h>

const char* skyeye_module = \"${MODULE_NAME}\";

extern void init_${MODULE_NAME}();

void module_init(){
	init_${MODULE_NAME}();
}

void module_fini(){
}" >> ${MODULE_FILE}

		# Create source code of device implementation
		echo -e "/* Copyright (C) 
* ${AUTHOR}
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/
/**
* @file ${MODULE_NAME}.c
* @brief The implementation of system controller
* @author ${AUTHOR}
* @version 78.77
*/

#include <skyeye_types.h>
#include <skyeye_sched.h>
#include <skyeye_signal.h>
#include <skyeye_class.h>
#include <skyeye_interface.h>
#include <skyeye_obj.h>
#include <skyeye_mm.h> 
#include <memory_space.h>
#include <skyeye_device.h>
#define DEBUG
#include <skyeye_log.h>

#include \"${MODULE_NAME}.h\"

static conf_object_t* new_${MODULE_NAME}(char* obj_name){
}
void free_${MODULE_NAME}(conf_object_t* dev){
	
}

void init_${MODULE_NAME}(){
	static skyeye_class_t class_data = {
		.class_name = \"${MODULE_NAME}\",
		.class_desc = \"${MODULE_NAME}\",
		.new_instance = new_${MODULE_NAME},
		.free_instance = free_${MODULE_NAME},
		.get_attr = NULL,
		.set_attr = NULL
	};

	SKY_register_class(class_data.class_name, &class_data);
}" >> ${SOURCE_FILE}
	else
		echo "Arg2 is wrong format,maybe you should delete the '/' after the directory $PREFIX_DIR"
	fi
else
	echo "Please prefix the directory which you want to gen the module into"
fi


